import pygame
import random

# 初始化 Pygame
pygame.init()

# 定义常量
WIDTH = 600
HEIGHT = 600
GRID_SIZE = 8
BLOCK_SIZE = WIDTH // GRID_SIZE
WHITE = (255, 255, 255)
COLORS = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255), (0, 255, 255)]

# 创建游戏窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("消消乐游戏")

# 初始化游戏网格
grid = [[random.randint(0, len(COLORS) - 1) for _ in range(GRID_SIZE)] for _ in range(GRID_SIZE)]

# 绘制网格
def draw_grid():
    for i in range(GRID_SIZE):
        for j in range(GRID_SIZE):
            color = COLORS[grid[i][j]]
            pygame.draw.rect(screen, color, (j * BLOCK_SIZE, i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
            pygame.draw.rect(screen, WHITE, (j * BLOCK_SIZE, i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 1)

# 检查是否有可消除的方块
def check_matches():
    matches = []
    # 检查水平方向
    for i in range(GRID_SIZE):
        for j in range(GRID_SIZE - 2):
            if grid[i][j] == grid[i][j + 1] == grid[i][j + 2]:
                for k in range(3):
                    if (i, j + k) not in matches:
                        matches.append((i, j + k))
    # 检查垂直方向
    for j in range(GRID_SIZE):
        for i in range(GRID_SIZE - 2):
            if grid[i][j] == grid[i + 1][j] == grid[i + 2][j]:
                for k in range(3):
                    if (i + k, j) not in matches:
                        matches.append((i + k, j))
    return matches

# 消除匹配的方块
def remove_matches(matches):
    for match in matches:
        i, j = match
        grid[i][j] = -1  # -1 表示该方块已消除

# 方块下落
def drop_blocks():
    for j in range(GRID_SIZE):
        empty_spaces = 0
        for i in range(GRID_SIZE - 1, -1, -1):
            if grid[i][j] == -1:
                empty_spaces += 1
            else:
                if empty_spaces > 0:
                    grid[i + empty_spaces][j] = grid[i][j]
                    grid[i][j] = -1
        # 填充顶部的空位
        for i in range(empty_spaces):
            grid[i][j] = random.randint(0, len(COLORS) - 1)

# 交换两个方块的位置
def swap_blocks(x1, y1, x2, y2):
    grid[x1][y1], grid[x2][y2] = grid[x2][y2], grid[x1][y1]
    matches = check_matches()
    if not matches:
        # 如果交换后没有匹配项，交换回来
        grid[x1][y1], grid[x2][y2] = grid[x2][y2], grid[x1][y1]
        return []
    return matches

# 得分
score = 0
font = pygame.font.Font(None, 36)

# 游戏主循环
clock = pygame.time.Clock()
running = True
selected_block = None
while running:
    screen.fill(WHITE)

    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            x, y = event.pos
            col = x // BLOCK_SIZE
            row = y // BLOCK_SIZE
            if selected_block is None:
                selected_block = (row, col)
            else:
                s_row, s_col = selected_block
                if (abs(row - s_row) == 1 and col == s_col) or (abs(col - s_col) == 1 and row == s_row):
                    matches = swap_blocks(s_row, s_col, row, col)
                    if matches:
                        remove_matches(matches)
                        score += len(matches)
                        drop_blocks()
                selected_block = None

    # 绘制网格
    draw_grid()

    # 显示得分
    score_text = font.render(f"得分: {score}", True, (0, 0, 0))
    screen.blit(score_text, (10, 10))

    # 更新显示
    pygame.display.flip()
    clock.tick(60)

# 退出游戏
pygame.quit()